#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QObject>
#include<QTcpServer>
#include<QTcpSocket>
#include<QMap>
#include<QString>
#include<QDebug>

class TcpServer : public QObject
{
    Q_OBJECT
public:
    explicit TcpServer(QObject *parent = nullptr, QTcpServer* server = nullptr, int port = 8080);

public:
    void start();
    void stop();
    QString getCleanIp(const QHostAddress &address);

private:
    int m_port;
    QMap<QString, QTcpSocket*> tcpSocketMap;
    QTcpServer *tcpServer;
signals:
    void listenSuccess(int port);
    void listenFailed(QString errMsg);
    void custom_connected(QString clientInfo);
    void custom_readyRead(QString source, QByteArray &data);
    void custom_errorOccurred(QAbstractSocket::SocketError);


public slots:
    // ---自定义槽函数处理tcpsocket 信号-----------------------------------
    // 这些槽函数处理具体业务，当需要ui界面时，发送信号即可，不要再ui界面操作这些业务的东西
    void do_newConnection();
    void do_connected();
    void do_readyRead();
    void do_disconnected();
    void do_destroyed();
    void do_errorOccurred(QAbstractSocket::SocketError);
    void do_write(const QString &msg);
    // ---自定义槽函数处理tcpsocket 信号-----------------------------------
};

#endif // TCPSERVER_H
